package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectGrade;
import com.EM1.www.dao.ConnectTeacher;
import com.EM1.www.po.Teacher;
import com.EM1.www.po.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/TeacherMain")
public class TeacherMain extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("user");
        ConnectTeacher connectTeacher=new ConnectTeacher();
        Teacher teacher;
        try {
            teacher = connectTeacher.selectTeacher(user.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ArrayList<Integer> grades=new ArrayList<>();
        ArrayList<Integer> grade = teacher.getGrade();
        ConnectGrade connectGrade=new ConnectGrade();
        try {
            ArrayList<Integer> allgrade= connectGrade.selectAllgrade();
            for (Integer gradei : grade) {
                for (Integer gradej : allgrade) {
                    if(gradej.intValue()==gradei.intValue()){
                        allgrade.remove(gradej);
                    }
                }
            }
            session.setAttribute("allgrade",allgrade);
            session.setAttribute("teacher",teacher);
            request.getRequestDispatcher("teacher.jsp").forward(request,response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
